package com.ctgu.springshiro.service.impl;
import com.ctgu.springshiro.dao.UserDao;
import com.ctgu.springshiro.entity.User;
import com.ctgu.springshiro.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService implements IUserService {

    @Autowired
    UserDao userDao;

    @Override
    public User findUserByName(String username) {
        return userDao.findByName(username);
    }

    @Override
    public User findUserById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public List<User> getAllUser() {
        return userDao.getAllUser();
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Map<String, Object> updateUser(String name, Integer id) {
        Map<String, Object> map = new HashMap<>();
        User user = findUserById(id);
        if(null == user){
            map.put("isUpdate", false);
            map.put("result", null);
        }else{
            user.setUsername(name);
            userDao.updateUser(user);
//            int b = 1/0;
            map.put("isUpdate", true);
            map.put("result", findUserById(id));
        }
        return map;
    }

    @Override
    public void deleteUser(User user) {
        userDao.deleteUser(user);
    }
}
